﻿
namespace LS.Nc6.Web.Entry.Areas.Systems.Controllers
{
    /// <summary>
    /// 文件管理
    /// </summary>
    [Route("[area]/[controller]/[action]")]
    [Area("Systems")]
    [ApiDescriptionSettings("Default")]
    public class FilesController : BaseAdminController
    {
        #region FilesController
        private readonly ISysFileService _fileService;
        private readonly ICache _cache;
        public FilesController(ISysFileService fileService, IOptions<CacheOptions> cacheOptions, Func<string, ISingleton, object> resolveNamed)
            : base(cacheOptions, resolveNamed)
        {
            _fileService = fileService;
            _cache = resolveNamed(cacheOptions.Value.CacheType.ToString(), default) as ICache;
        }
        #endregion
        #region 页面
        public ActionResult Index()
        {
            SysPower(SysMenuConstant.MENU_FILES_MANAGE, SysMenuConstant.FUN_SELECT, UserInfo.ID.ToString());
            return View();
        }
      
       
        #endregion

        #region  公共方法
       

        [HttpGet]
        public async Task<ResponseResultDataList> GetAllFiles()
        {
            string bizetype = RequestHelper.FilterParam(Request.Query["bizetype"]);
            string foname = RequestHelper.FilterParam(Request.Query["foname"]);
            var page = Request.Query["page"].ToString().GetQueryValueOrNull<int>(1);
            var limit = Request.Query["limit"].ToString().GetQueryValueOrNull<int>(20);
            string field = Request.Query["field"];
            string order = Request.Query["order"];

            var expable = Expressionable.Create<SysFileEntity>();
            expable.AndIF(!string.IsNullOrEmpty(bizetype), a => a.Bizetype == bizetype);
            expable.AndIF(!string.IsNullOrEmpty(foname), a => a.Foname == foname);
            var exp = expable.ToExpression();
            var strField = RequestHelper.FilterParam(field);
            var strOrder = RequestHelper.FilterParam(order);

            Expression<Func<SysFileEntity, object>> expressionOrder;
            if (string.IsNullOrEmpty(strField) || string.IsNullOrEmpty(strOrder))
            {
                expressionOrder = (a) => new { a.Id };
            }
            else
            {
                //实现字段排序
                expressionOrder = (a) => new { a.CreateDate };
            }
            var result =await _fileService.PageList(exp, page, limit, expressionOrder);
            return new ResponseResultDataList { code = ResponseHelper.SEUCCESSCODE, message = "请求成功", data = result.Items, count = result.TotalCount };
        }

        [HttpGet]
        public async Task<ResponseDataResult> CustomerFileList(string ids)
        {
            var result = await  _fileService.CustomerFileList(ids);
            return new ResponseDataResult { code = ResponseHelper.SEUCCESSCODE, message = "请求成功", data = result };
        }

        [HttpPost]
        public async Task<ResponseResult> DelFile(string ids)
        {
            var r = SysPower(SysMenuConstant.MENU_DICTIONARY_MANAGE, SysMenuConstant.FUN_DELETE, UserInfo.ID.ToString(), false);
            if (!r)
            {
                return ResponseHelper.MessageResult(ResponseHelper.PERMISSION_WSCQX);
            }
            int result =await _fileService.DeleteAsync(ids.ToString());
            return ResponseHelper.DeleteResult(result);
          
        }

        #endregion


        #region 私有方法

        #endregion
    }
}